package com.zd.kids.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.gyf.immersionbar.BarHide;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.hyy.libray.presenter.IBasePresenter;
import com.hyy.libray.ui.activity.BaseActivity;
import com.zd.kids.DataBean.CalendarTimeDataBean;
import com.zd.kids.R;
import com.zd.kids.common.ToastUtils;
import com.zd.kids.net.CustomObserver;
import com.zd.kids.net.HttpMethod;
import com.zd.kids.ui.adapter.CalendarClassTimeAdapter;
import com.zd.kids.ui.view.JoinClassPopupWindow;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CalendarActivity extends BaseActivity {

    @BindView(R.id.calendarView)
    CalendarView calendarView;

    @BindView(R.id.recycle_view)
    RecyclerView recycleView;

    @BindView(R.id.tv_1)
    TextView tv1;

    @BindView(R.id.tv_2)
    TextView tv2;

    @BindView(R.id.tv_3)
    TextView tv3;

    @BindView(R.id.tv_cur_date)
    TextView tvCurDate;

    @BindView(R.id.tv_teacher_name)
    TextView tvTeacherName;

    @BindView(R.id.start_class)
    TextView tvStartClass;


    /**
     * 获取布局文件地址
     */
    @Override
    public int getLayoutResId() {
        return R.layout.activity_calendar;
    }

    /**
     * 初始化布局
     */
    @Override
    public void initView() {
        initTitle(getString(R.string.has_reservation_class), R.color.title_top_color);

        calendarView.setWeeColor(this.getResources().getColor(R.color.title_top_color), 0xffffffff);

        initRecycleView();

        getCanlendarData();
    }

    /**
     * 获取Presenter，不用MVP，可以为空
     *
     * @return
     */
    @Override
    protected IBasePresenter onBasePresenter() {
        return null;
    }

    private Calendar getSchemeCalendar(int year, int month, int day, String text) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setScheme(text);
        calendar.setSchemeColor(0xFF40db25);
        calendar.addScheme(new Calendar.Scheme());
        return calendar;
    }


    private CalendarClassTimeAdapter calendarClassTimeAdapter;

    private void initRecycleView() {

        calendarClassTimeAdapter = new CalendarClassTimeAdapter(new ArrayList<>());
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setAdapter(calendarClassTimeAdapter);
    }

    JoinClassPopupWindow mJoinClassPopupWindow;

    @OnClick({R.id.prepar_class, R.id.start_class, R.id.img_left, R.id.img_right, R.id.tv_1, R.id.tv_2, R.id.tv_3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_left:
                calendarView.scrollToPre();
                break;
            case R.id.img_right:
                calendarView.scrollToNext();
                break;
            case R.id.tv_1:
                initTvType(1);

                showCalendarData(0);
                break;
            case R.id.tv_2:
                initTvType(2);
                showCalendarData(1);
                break;
            case R.id.tv_3:
                initTvType(3);
                showCalendarData(2);
                break;
            case R.id.prepar_class:
                Intent intent = new Intent(this, HelpActivity.class);
                startActivity(intent);
                break;
            case R.id.start_class:
                String curScheme = calendarView.getSelectedCalendar().getScheme();
                if (!TextUtils.isEmpty(curScheme)) {

                    String psw = mCalendarTimeDataBeans.get(mCutShowIndex).getPasswd();

                    String roomId = mCalendarTimeDataBeans.get(mCutShowIndex).getRoomid();

                    String webPath = mCalendarTimeDataBeans.get(mCutShowIndex).getWeburl();

                    if (mJoinClassPopupWindow == null) {
                        mJoinClassPopupWindow = new JoinClassPopupWindow(this, roomId, psw, webPath);
                    }

                    mJoinClassPopupWindow.setLiveSchemeDatas(roomId, psw, webPath);

                    mJoinClassPopupWindow.showView();
                } else {
                    ToastUtils.getIntence().makeTextShow(getString(R.string.do_not_reservation_class));
                }


                break;
        }
    }


    private void initTvType(int index) {

        tv1.setBackgroundResource(R.drawable.shape_calendar_type_default);
        tv2.setBackgroundResource(R.drawable.shape_calendar_type_default);
        tv3.setBackgroundResource(R.drawable.shape_calendar_type_default);

        if (index == 1) {
            tv1.setBackgroundResource(R.drawable.shape_calendar_type_select);
        } else if (index == 2) {
            tv2.setBackgroundResource(R.drawable.shape_calendar_type_select);
        } else if (index == 3) {
            tv3.setBackgroundResource(R.drawable.shape_calendar_type_select);
        }

    }


    private List<CalendarTimeDataBean> mCalendarTimeDataBeans;

    private void getCanlendarData() {
        HttpMethod.getIntence().getCalendarData(new CustomObserver<List<CalendarTimeDataBean>>(this) {
            @Override
            public void onSuccess(List<CalendarTimeDataBean> calendarTimeDataBeans) {
                mCalendarTimeDataBeans = calendarTimeDataBeans;

                showCalendarData(0);

            }

            @Override
            public void onError(String errorCode) {

            }
        });

    }

    private int mCutShowIndex;

    private void showCalendarData(int dex) {
        mCutShowIndex = dex;
        tvStartClass.setBackgroundResource(R.drawable.shape_bt_bg_startclass_gray);
        tvStartClass.setEnabled(false);
        tvTeacherName.setText("");
        if (mCalendarTimeDataBeans == null || mCalendarTimeDataBeans.size() <= dex) {

            calendarClassTimeAdapter.setNewData(new ArrayList<>());

            getMapCalendaData(new ArrayList<>());

            return;
        }


        tvTeacherName.setText(mCalendarTimeDataBeans.get(dex).getTeacher());
        List<Long> timeDate = mCalendarTimeDataBeans.get(dex).getBookingLongdate();
        long curTime = System.currentTimeMillis();
        for (Long mil : timeDate) {
            if (mil > curTime && (mil - curTime) <= 1 * 60 * 60 * 1000) {
                tvStartClass.setBackgroundResource(R.drawable.shape_bt_bg_startclass);
                tvStartClass.setEnabled(true);
                break;
            }
        }

        calendarClassTimeAdapter.setNewData(timeDate);

        getMapCalendaData(mCalendarTimeDataBeans.get(dex).getBookingdate());

    }


    private void getMapCalendaData(List<String> datas) {
        Map<String, Calendar> map = new HashMap<>();
        for (int i = 0; i < datas.size(); i++) {

            int year = 0;
            int month = 0;
            int day = 0;

            String[] dates = datas.get(i).split("-");
            year = Integer.parseInt(dates[0]);
            month = Integer.parseInt(dates[1]);
            day = Integer.parseInt(dates[2]);

            map.put(getSchemeCalendar(year, month, day, 10 + "").toString(),
                    getSchemeCalendar(year, month, day, 10 + ""));
        }

        //此方法在巨大的数据量上不影响遍历性能，推荐使用
        calendarView.clearSchemeDate();
        calendarView.setSchemeDate(map);
        tvCurDate.setText(calendarView.getCurYear() + "/" + calendarView.getCurMonth());
        calendarView.setOnMonthChangeListener(new CalendarView.OnMonthChangeListener() {
            @Override
            public void onMonthChange(int year, int month) {
                tvCurDate.setText(year + "/" + month);
            }
        });
    }

}
